import { getStore, setStore } from '@/utils/storage'
import { getUserInfo, logout } from '@/mock/index'
const state = {
  token: getStore('token'),
  // 用户信息
  userInfo: {},
  userId: '',
  userName: '',
  avatar: '',
  roleName: '',
  status: ''
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    setStore('token', token)
  },
  SET_USER: (state, user) => {
    state.userInfo = user
    state.userId = user.uuid
    state.userName = user.userName
    state.avatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
    state.roleName = user.roleName
    state.status = user.status
  }
}
const actions = {
  // 获取用户信息
  async getUserInfo ({ commit, state }) {
    // 用户信息
    const res = await getUserInfo()
    if (res.code === '0000') {
      commit('SET_USER', res.data)
    }
  },
  // 前端退出登录
  async resetToken ({ commit, state, dispatch }) {
    localStorage.clear()
    commit('SET_TOKEN', '')
    commit('SET_USER', {})
    dispatch('tagsView/delAllViews', null, { root: true })
  },

  // 退出登录
  async logout ({ commit, state, dispatch }) {
    const res = await logout()
    if (res.code === '0000') {
      localStorage.clear()
      commit('SET_USER', {})
      dispatch('tagsView/delAllViews', null, { root: true })
    }
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
